package dp.onClass.class3.exer01;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        int[] dp = new int[n];
        Arrays.fill(dp, 1);
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (arr[j] <= arr[i]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
        }
        int maxIndex = -1;
        int maxLen = -1;
        for (int i = 0; i < n; i++) {
            if (maxLen <= dp[i]) {
                maxIndex = i;
                maxLen = dp[i];
            }
        }
        System.out.println(maxLen);
        int count = maxLen;
        if (count == 1) {
            System.out.println(arr[maxIndex]);
            return;
        } else {
            System.out.print(arr[maxIndex] + " ");
        }
        count--;
        int prev = arr[maxIndex];
        for (int i = maxIndex - 1; i >= 0; i--) {
            if (count > 0 && arr[i] <= prev && dp[i] == count) {
                if (count == 1) {
                    System.out.println(arr[i]);
                } else {
                    System.out.print(arr[i] + " ");
                }
                count--;
                prev = arr[i];
            }
        }
    }
}
